﻿

int pwdchg(int argc, char** argv)
{
	struct passwd *userlist;
	int count, usernumber;
	FILE *tmp, *stmp, *mailer, *profile;
	char *commentfield, *username, *userdir, *home;
	char *mailcomment, *mailemail, reply;

	commentfield = (char*)malloc(1024*sizeof(char));
	username = (char*)malloc(8*sizeof(char));
	userdir = (char*)malloc(256*sizeof(char));
	home = (char*)malloc(256*sizeof(char));
	mailcomment = (char*)malloc(1024*sizeof(char));
	mailemail = (char*)malloc(512*sizeof(char));

	if (argc != 4)
	{

		printf("usage: %s [ dirname - no slashes ] [ logname ] [ comment - in quotes ]\n", argv[0]);
		return 1;
	}

	if (strlen(argv[2]) > 8 || strlen(argv[2]) < 5)
	{
		printf("Please choose a logname between 5 and 8 characters!\n");
		return 1;
	}

	signal(SIGHUP, SIG_IGN);
	signal(SIGINT, SIG_IGN);

	setpwent();

	count = 0;

	while ((userlist = getpwent()) != NULL)
	{
		if (count < userlist->pw_uid)
		{
			count = userlist->pw_uid;
			usernumber = count + 1;
		}
	}

	endpwent();

	sprintf(commentfield, "%s", argv[3]);
	sprintf(userdir, "%s", argv[1]);
	sprintf(username, "%s", argv[2]);
	sprintf(home, "/%s/%s", argv[1], argv[2]);

	printf("/n");
	printf("Check this out before proceeding!!!\n");
	printf("---------------\n");
	printf("Logname:\t%s\n", username);
	printf("Homedir:\t/%s/%s\n", userdir, username);
	printf("Comment:\t%s\n", commentfield);
	printf("---------------\n");
	printf("\n");

	printf("\n");
	printf("All of this ok?\n");
	printf("\n");
	printf("y or n [n is the default]\n");
	printf("\n");

	scanf("%c", &reply);

	if (reply != 'y')
	{
		printf("\n");
		printf("All right, give it another shot if you want!\n");
		return 0;
	}

	tmp = fopen("/etc/passwd", "a");
	fprintf(tmp, "%s:x:%d:1:%s:/%s/%s:/bin/sh\n", username, usernumber, commentfield, userdir, username);
	fclose(tmp);

	 mkdir(home, 0755);
	 chdir(home);

	 profile = fopen(".profile", "a");
	 fprintf(profile, "stty istrip\n");
	 fprintf(profile, "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n");
	 fprintf(profile, "export PATH\n");
	 fprintf(profile, "\n\n");
	 fclose(profile);

	 chown(home, usernumber, 1);
	 chown(".profile", usernumber, 1);
	 chmod(".profile", 0644);

	 if ((mailer = popen("/usr/lib/sendmail -t", "w")) == NULL)
	 {
		 perror("Mailer");
		 return 1;
	 }

	 sprintf(mailcomment, "%s\n", commentfield);
	 sprintf(mailemail, "The email address is %s@host.com!\n", username);


	 fputs("To: bob@host.com\n", mailer);
	 fputs("Subject: New User Added!!!\n", mailer);
	 fputs("\n", mailer);
	 fputs(mailcomment, mailer);
	 fputs("has a new account set up!\n", mailer);
	 fputs("\n", mailer);
	 fputs("Thank you, \n", mailer);
	 fputs("\t Johny Ma\n", mailer);
	 pclose(mailer);

	 printf("\nAll Done!!!\n");
	 printf("\nNow set the Password!\n");
	 printf("\n");
	 execl("/usr/bin/passwd", "passwd", username, NULL);
	 printf("\nPassword set!!! Take a break...\n");

	 return 2;
}